﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using CYTS.Aoyou.Framework.Log;

namespace CYTS.Aoyou.Framework.Cache
{
    /// <summary>
    /// 本地缓存帮助类
    /// </summary>
    public class LocalCacheHelper
    {
        public void Add(string key, object value, TimeSpan expiration)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            try
            {
                objCache.Insert(key, value, null, DateTime.Now.Add(expiration), new TimeSpan(0));
            }
            catch(Exception ex)
            {
                string msg = "新增缓存数据时出错";
                //Logger.LogError(msg, ex);
                throw new Exception(msg, ex);
            }
        }

        public void Add(string key, object value)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            try
            {
                objCache.Insert(key, value); 
            }
            catch (Exception ex)
            {
                string msg = "新增缓存数据时出错";
                //Logger.LogError(msg, ex);
                throw new Exception(msg, ex);
            }
        }

        public object Get(string key)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            object result = null;
            try
            {
                result = objCache.Get(key); 
            }
            catch (Exception ex)
            {
                string msg = "获取缓存数据时出错";
                //Logger.LogError(msg, ex);
                throw new Exception(msg, ex);
            }

            return result;
        }

        public void Remove(string key)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            try
            {
                objCache.Remove(key);
            }
            catch (Exception ex)
            {
                string msg = "删除缓存数据时出错";
                //Logger.LogError(msg, ex);
                throw new Exception(msg, ex);
            }
        }
    }
}
